package com.cn.ant.modules.user.web;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.utils.AjaxJson;
import com.cn.ant.common.utils.StringUtils;
import com.cn.ant.common.web.BaseController;
import com.cn.ant.modules.user.entity.UserMain;
import com.cn.ant.modules.user.service.UserMainService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户信息Controller
 * @author 黄根华
 * @version 2015-09-26
 */
@Controller
@RequestMapping(value = "${adminPath}/user/userMain")
public class UserMainController extends BaseController {

	@Autowired
	private UserMainService userMainService;
	
	@ModelAttribute
	public UserMain get(@RequestParam(required=false) String id) {
		UserMain entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = userMainService.get(id);
		}
		if (entity == null){
			entity = new UserMain();
		}
		return entity;
	}
	
	@RequiresPermissions("user:userMain:view")
	@RequestMapping(value = {"list", ""})
	public String list(UserMain userMain, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<UserMain> page = userMainService.findPage(new Page<UserMain>(request, response), userMain); 
		model.addAttribute("page", page);
		return "modules/user/userMainList";
	}

	@RequiresPermissions("user:userMain:view")
	@RequestMapping(value = "form")
	public String form(UserMain userMain, Model model) {
		model.addAttribute("userMain", userMain);
		return "modules/user/userMainForm";
	}

	@RequiresPermissions("user:userMain:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public AjaxJson save(UserMain userMain, HttpServletRequest request,Model model) {
		AjaxJson jo = new AjaxJson();
		try {
			if (!beanValidator(model, userMain)) {
                jo.setSuccess(false);
                jo.setMsg("数据校验失败,请重试!");
                return jo;
			} else {
			    userMainService.save(userMain);
			}
			msg = "保存用户信息成功";
            jo.setMsg(msg);
            jo.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
            jo.setSuccess(false);
            jo.setMsg("操作失败,请联系管理员");
		}
		return jo;
	}
	
	@RequiresPermissions("user:userMain:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public AjaxJson delete(UserMain userMain, RedirectAttributes redirectAttributes) {
		AjaxJson jo = new AjaxJson();
        if (StringUtils.isNotBlank(userMain.getId())) {
            userMainService.delete(userMain);
            jo.setMsg("删除用户信息成功");
        } else {
            jo.setSuccess(false);
            jo.setMsg("参数错误,删除失败");
        }
        return jo;
	}

}